package org.flexlancer.datagrid
{
	import flash.events.Event;
	import flash.utils.setTimeout;
	
	import mx.controls.DataGrid;
	
	import org.flexlancer.Logger.Logger;

	public class AutoGrowDataGrid extends DataGrid
	{
		public static const GROW_COMPLETE:String = "growComplete";
		public function AutoGrowDataGrid()
		{
			this.wordWrap = true;
			this.variableRowHeight = true;
			super();
		}
		
		override public function set dataProvider(value:Object):void{
			try{
				super.dataProvider = value;
				if ( value != null )
					setTimeout(this.SetGrowHeight,1000);
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		private function SetGrowHeight():void{
			try{
				if ( this.maxVerticalScrollPosition > 0 ){
					this.height +=20;
					setTimeout(this.SetGrowHeight,100);
				}else{
					this.dispatchEvent(new Event(GROW_COMPLETE));
				}
			}catch(err:Error){
				throw err;
			}
		}
	}
}